package org.web.demo.service.impl;


import lombok.extern.slf4j.Slf4j;
import org.web.demo.common.qo.PageResult;
import org.web.demo.common.qo.QueryObject;
import org.web.demo.dao.IUserDAO;
import org.web.demo.dao.impl.UserDAOImpl;
import org.web.demo.entity.User;
import org.web.demo.service.IUserService;

import java.util.Collections;
import java.util.List;


/**
 * @author yexi
 */
@Slf4j
public class UserServiceImpl implements IUserService {
    private IUserDAO userDAO = new UserDAOImpl();

    @Override
    public User login(String username, String password) {
        User user = userDAO.checkUser(username);
        if (user == null) {
            throw new RuntimeException("账号不存在");
        }
        if (!user.getPassword().equals(password)) {
            throw new RuntimeException("密码错误");
        }
        return user;
    }

    @Override
    public PageResult query(QueryObject qo) {
        IUserDAO userDAO = new UserDAOImpl();
        int i = userDAO.selectCount(qo);
        log.info(String.valueOf(i));
        if (i == 0) {
            return new PageResult(qo.getCurrentPage(), qo.getPageSize(), i, Collections.emptyList());
        }
        List<User> list = userDAO.query(qo);
        return new PageResult(qo.getCurrentPage(), qo.getPageSize(), i, list);
    }

}
